﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;

namespace HPPClientLibrary
{
    public abstract class HashCalculatorBase : IHashCalculator
    {
        private HashAlgorithm _algorithm;

        protected HashCalculatorBase(HashAlgorithm algorithm)
        {
            _algorithm = algorithm;
        }

        public virtual string CalcFile(string pathAndFileName)
        {
            try
            {
                FileStream getFile = new FileStream(pathAndFileName, FileMode.Open, FileAccess.Read, FileShare.Read);
                
                byte[] byteHash = _algorithm.ComputeHash(getFile);

                string stringHash = BitConverter.ToString(byteHash);
                stringHash = stringHash.Replace("-", "");
                return stringHash;
            }
            catch (Exception ex)
            {
                CommonLogger.Logger.Warn("CalcFile()", ex);
                return null;
            }
        }
    }
}
